package com.chezai.client.activitys;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chezai.client.R;
import com.chezai.client.adapter.DeployAdapter;
import com.chezai.client.bean.TerminalParameter;
import com.chezai.client.dao.manager.TerminalParameterDaoManager;
import com.chezai.client.global.Const;
import com.chezai.client.utils.PreferenceManager;

import java.util.List;

import butterknife.BindView;

/**
 * 配置信息页
 * Created by likunyang on 2017/9/12.
 */

public class DeployInfoActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.btn_back)
    Button btnBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.btn_update_time)
    Button btnUpdate;
    private DeployAdapter mAdapter;
    private String type;

    @Override
    public int getLayoutId() {
        return R.layout.activity_deploy;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        mAdapter = new DeployAdapter(R.layout.item_deploy);
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    public void initData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null && bundle.containsKey(Const.BUNDLE_KEY_PARAM_TYPE)) {
            type = bundle.getString(Const.BUNDLE_KEY_PARAM_TYPE);
            if (TextUtils.equals(TerminalParameter.TERMINAL_PARAM, type)) {
                tvTitle.setText("配置信息");
            } else {
                tvTitle.setText("状态信息");
                btnUpdate.setVisibility(View.VISIBLE);
            }
            List<TerminalParameter> allTerminalParameter = TerminalParameterDaoManager.getInstance().findTerminalParameterByType(type);
            mAdapter.setNewData(allTerminalParameter);
        }
    }

    @Override
    public void setListener() {
        super.setListener();
        btnBack.setOnClickListener(this);
        btnUpdate.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_update_time:
                TerminalParameterDaoManager.getInstance().updateTerminalParameterById(Const.TermianlParameter.TAKE_PHOTO_INTERVAL_ID, "10");
                PreferenceManager.putInt(Const.SP_INTERVAL_TIME, 10);
                if (!TextUtils.equals(TerminalParameter.TERMINAL_PARAM, type)) {
                    List<TerminalParameter> allTerminalParameter = TerminalParameterDaoManager.getInstance().findTerminalParameterByType(type);
                    mAdapter.setNewData(allTerminalParameter);
                }
                break;
        }

    }
}
